﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YB
{
    public partial class Switch : UserControl
    {
        private Color _capsuleBackgroundColor = Color.Transparent;
        [Browsable(true)]
        [Category("开关")]
        [Description("关闭显示文字")] 
        public string LeftText = "千克";

        [Browsable(true)]
        [Category("开关")]
        [Description("打开显示文字")]
        private string rightText = "克";
        public string RightText
        {
            get { return rightText; }
            set {
                rightText=value;
                Invalidate();
            }
        }
        [Browsable(true)]
        [Category("开关")]
        [Description("开关背景颜色")]
        public Color CapsuleBackgroundColor
        {
            get { return _capsuleBackgroundColor; }
            set
            {
                _capsuleBackgroundColor = value;
                Invalidate();
            }
        }
        private bool _checked = false;
        public bool Checked
        {
            get { return _checked; }
            set
            {
                _checked = value;
                Invalidate();
            }
        }
        private Color _bakccolor = Color.Red;
        public Switch()
        {
            InitializeComponent();
            _bakccolor = this.BackColor;
            this.BackColor = Color.Transparent;

        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;

            int width = ClientSize.Width;
            int height = ClientSize.Height;
            int radius = height / 2; // 半径为窗口高度的一半
            int middleY = height / 2; // 胶囊的中心Y坐标

            // 绘制胶囊的左半边边线
            g.DrawArc(Pens.Black, new Rectangle(0, middleY - radius, radius * 2, radius * 2), 90, 180);

            // 绘制胶囊的右半边边线
            g.DrawArc(Pens.Black, new Rectangle(width - radius * 2, middleY - radius, radius * 2, radius * 2), 270, 180);
            Brush bakbrushes = new SolidBrush(_capsuleBackgroundColor);
            g.FillEllipse(bakbrushes, 0, middleY - radius, radius * 2, radius * 2);
            g.FillEllipse(bakbrushes, width - radius * 2, middleY - radius, radius * 2, radius * 2);
            g.FillRectangle(bakbrushes, radius, middleY - radius, width - radius * 2, height);
            if (!_checked)
            {
                // 绘制胶囊的左半边填充
                g.FillEllipse(Brushes.Red, 0, middleY - radius, radius * 2, radius * 2);
                g.FillRectangle(Brushes.Red, radius, middleY - radius, width / 2 - radius, height);
            }
            else
            {
                // 绘制胶囊的右半边填充
                g.FillEllipse(Brushes.Green, width - radius * 2, middleY - radius, radius * 2, radius * 2);
                g.FillRectangle(Brushes.Green, width / 2, middleY - radius, width / 2 - radius, height);
            }
            // 绘制两个半圆连接处的直线
            g.DrawLine(Pens.Black, radius, middleY - radius, width - radius, middleY - radius);
            g.DrawLine(Pens.Black, radius, middleY + radius, width - radius, middleY + radius);
            

            Font font = this.Font;
            Rectangle leftRect = new Rectangle(radius/2 , middleY - radius , width / 2 - radius/2, height);
            Rectangle rightRect = new Rectangle(width / 2 , middleY - radius , width / 2 - radius/2, height);
            
            DrawText(g, font, leftRect, this.LeftText);
            DrawText(g, font, rightRect, this.RightText);
        }
        private void DrawText(Graphics graphics, Font font, RectangleF rect, string text, Brush color =null)
        {
            // 检查文本是否超出区域
            Size textSize = graphics.MeasureString(text, font).ToSize();
            if (textSize.Width > rect.Width || textSize.Height > rect.Height)
            {
                // 缩小字体以适应区域
                while (textSize.Width > rect.Width || textSize.Height > rect.Height)
                {
                    font = new Font(font.FontFamily, font.Size - 0.1f);
                    textSize = graphics.MeasureString(text, font).ToSize();
                }
            }
            if (color == null)
            {
                color = Brushes.White;
            }
            font = new Font(font, FontStyle.Bold);

            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            format.FormatFlags = StringFormatFlags.NoWrap; // 设置文本不换行

            // 绘制文本
            graphics.DrawString(text, font, color, rect, format);
        }

        private void Switch_Click(object sender, EventArgs e)
        {
            _checked = !_checked;
            Invalidate();
        }
    }
}
